package com.example.jsondemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        test();
    }

    private void test() {
        // 创建对象数据
        Log.v("Test", "==== 原始对象====");
        List<User> users= new ArrayList<User>();
        User user1 = new User();
        user1.setAge(20);
        user1.setAddress("延安路局");
        user1.setName("小明");
        user1.setLike("羽毛球");
        user1.setSex("男");
        user1.show();

        users.add(user1);

        User user2 = new User();
        user2.setAge(21);
        user2.setAddress("下城区");
        user2.setName("小蓝");
        user2.setLike("舞蹈");
        user2.setSex("女");
        user2.show();

        users.add(user2);

        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            buffer.append(user.show() + "\n");
        }
        Log.v("List", buffer.toString());

        // 对象转json
        Log.v("Test", "==== FastJson 对象转 json字符串====");
        String user1Json = JSON.toJSONString(user1);
        Log.v("FastJson", user1Json);
        String user2Json = JSON.toJSONString(user2);
        Log.v("FastJson", user2Json);
        String usersJson = JSON.toJSONString(users);
        Log.v("FastJson", usersJson);

        Log.v("Test", "==== Gson 对象转 json字符串====");
        Gson gson = new Gson();
        String user1Gson = gson.toJson(user1);
        Log.v("Gson", user1Gson);
        String user2Gson = gson.toJson(user2);
        Log.v("Gson", user2Gson);
        String usersGson = gson.toJson(users);
        Log.v("Gson", usersGson);

        // json 转对象
        Log.v("Test", "==== FastJson json字符串 转对象====");
        User user11 = JSON.parseObject(user1Json, User.class);
        user11.show();
        User user22 = JSON.parseObject(user2Json, User.class);
        user22.show();
        List<User> users1122 = JSON.parseArray(usersJson, User.class);

        buffer = new StringBuffer();
        for (int i = 0; i < users1122.size(); i++) {
            User user = users.get(i);
            buffer.append(user.show() + "\n");
        }
        Log.v("List", buffer.toString());

        Log.v("Test", "==== Gson json字符串 转对象====");
        User user111 = gson.fromJson(user1Gson, User.class);
        user111.show();
        User user222 = gson.fromJson(user2Gson, User.class);
        user222.show();
        List<User> users111222 = gson.fromJson(usersGson, new TypeToken<List<User>>(){}.getType());

        buffer = new StringBuffer();
        for (int i = 0; i < users111222.size(); i++) {
            User user = users.get(i);
            buffer.append(user.show() + "\n");
        }
        Log.v("List", buffer.toString());
    }
}
